<?php
class Recipients_model extends CI_Model {

	function getRecipients($search="",$start_row='',$limit_no=''){
		$email = isset($_SESSION['email'])? $_SESSION['email']:'';
		$limit = ($start_row == '' && $limit_no == '')? "":"LIMIT $start_row,$limit_no";
		$query = $this->db->query("SELECT a.* FROM mlme_recipients a,mlme_users b WHERE a.userid = b.userid AND b.email = '".$email."' $search ORDER BY a.fname $limit");
		return $query->result();
	}
	function getMsgRecipients( $messageid ){
		if(!$messageid) return;
		$qrystr = "
				SELECT b.*
				FROM mlme_messages_recipients_x a, mlme_recipients b
				WHERE a.messageid = $messageid AND a.recipientid = b.recipientid ORDER BY fname ASC";
		$query = $this->db->query($qrystr);
		return $query->result();
	}
	function addRecipients(){
		$userid = $this->get_userid($_SESSION['email']);
		$type = $this->input->post('recipient');

		$new_recipients = array(

			'userid' => $userid,
			'fname' => $this->input->post('add-recep-fname'),
			'lname' => $this->input->post('add-recep-lname'),
			'mname' => $this->input->post('add-recep-mname'),
			'email' => $this->input->post('add-recep-email'),
			'state' => ($this->input->post('add-recep-state') == 'State')? '':$this->input->post('add-recep-state'),
			'city' => ($this->input->post('add-recep-city') == 'City')? '':$this->input->post('add-recep-city'),
			'zip' => ($this->input->post('add-recep-zip') == 'Zip')? '':$this->input->post('add-recep-zip'),
			'phone' => $this->input->post('add-recep-phone'),
			'is_executor' => $this->input->post('is_executor'),
			'date_added' => date('F j, Y'),			'relationship' => $this->input->post('add-recep-rel'),
			'status' => 1
		 	);
			

		$insert = $this->db->insert('mlme_recipients', $new_recipients);
		return $insert;
	}

	function get_userid($email){
		 $query = $this->db->query("SELECT userid FROM mlme_users WHERE email ='".$email."'");
		 $result = $query->row();
		 return $result->userid;
	}
	function get_relationships(){
		$query = $this->db->query("SELECT * FROM mlme_relationships");
		return $query->result_array();
	}
	function delete_recipient($id){
		$delete = $this->db->query("DELETE FROM mlme_recipients WHERE recipientid = $id");
		return $delete;
	}
	function get_recipient_info($id){
		$query = $this->db->query("SELECT * FROM mlme_recipients WHERE recipientid = ".$id);
		return $query->row_array();
	}
	function update_recipient($post){
		$userid = $this->get_userid($_SESSION['email']);
		$rec_id = urldecode($this->encryption_lib->decode($post['edit-recep-id']));
		$data = array(
			'fname' => $post['add-recep-fname'],
			'lname' => $post['add-recep-lname'],
			'mname' => $post['add-recep-mname'],
			'email' => $post['add-recep-email'],
			'state' => $post['add-recep-state'],
			'city' => $post['add-recep-city'],
			'zip' => $post['add-recep-zip'],
			'phone' => $post['add-recep-phone'],
			'relationship' => $post['add-recep-rel'],
			'is_executor' => $this->input->post('is_executor'),
		);
		$this->db->where('userid',$userid);
		$this->db->where('recipientid',$rec_id);
		$update = $this->db->update('mlme_recipients',$data);
		return ($update)? true:false;
	}
	function check_email_recipient_exist($email){
		$userid = $this->get_userid($_SESSION['email']);
		$query = $this->db->query("SELECT email FROM mlme_recipients WHERE email='".$email."' AND userid = ".$userid);
		return ($query->num_rows() > 0)? true:false;
	}
	
	//get all user profiler to display at executor
	function executorProfiler($userId){
		$email = isset($_SESSION['email'])? $_SESSION['email']:'';
		$sql = 'SELECT a.*,b.* FROM mlme_users as a LEFT JOIN mlme_recipients as b ON a.userid=b.userid WHERE  b.email="'.$email.'" AND is_executor=1';
		$query = $this->db->query($sql);
		return ($query->num_rows()>0) ? $query->result_array() : '';
	}
	//get all recipients
	function getAllRecipientsEmail($userId){		
		$sql = 'SELECT * FROM mlme_recipients WHERE userid="'.$userId.'" AND is_executor=0';
		$query = $this->db->query($sql);
		return ($query->num_rows()>0) ? $query->result_array() : '';
	}

}// end of Model


